package com.qinggeng.project.api.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import com.qinggeng.common.enums.OrdersDistTypeEnum;
import com.qinggeng.common.enums.PayTypeEnum;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.project.system.domain.*;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.qinggeng.common.enums.OrdersStatusCodeEnum.getOrdersStatusCode;

/**
 * 订单列表
 */
@Data
public class OrdersInfoDto {

    //订单ID
    private Long ordersId;
    //订单编号
    private String ordersNo;

    //创建时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    //配送方式（10=配送 20=自提）
    private String distType;

    //订单类型  普通订单("10"),免费送("20"),拼团订单("30"),商家补贴("40"),捶一捶("50"),好友助力("60")
    private String ordersType;

    /**
     *  状态码
     *  00=未知
     *  10=待付款，20=待发货，30=待收货，40=待评价，50=已完成，
     *  60=已取消，70=支付超时，80=退单中，90=已退单，100=退单失败
     */
    private String statusCode;
    //状态
    private String status;

    //商品总价
    private BigDecimal totalPrice;
    //支付金额
    private BigDecimal payPrice;
    //优惠金额
    private BigDecimal reducePrice;
    //配送费
    private BigDecimal distPrice;
    //支付方式
    private String payType;
    //退款金额
    private BigDecimal payment;
    //备注
    private String remark;

    //支付超时时间
    private Long payOutTime;

    //发货时间
    private String shipTime;

    //预计到达时间
    private String arriveTime;

    //送达时间（签收时间）
    private String distTime="";

    //确认收货时间
    private String affirmTime;

    //商品列表
    private List<AddOrdersInfoGoodsDto> goodsList;

    private List<OrdersGift> giftList;

    //收货地址
    private OrdersAddress address;

    //配送点
    private OrdersPoint point;

    //骑手
    private OrdersCourier courier;

    //签收人姓名
    private String receiver;

    //店铺
    private Shop shop;

    //签收状态
    private String distStatus;




    public String getPayType() {
        if (this.payType == null){
            return "未支付";
        }
        return PayTypeEnum.valuesOf(this.payType).getInfo();
    }

    public Long getOrdersId() {
        return ordersId;
    }

    public void setOrdersId(Long ordersId) {
        this.ordersId = ordersId;
    }

    public String getOrdersNo() {
        return ordersNo;
    }

    public void setOrdersNo(String ordersNo) {
        this.ordersNo = ordersNo;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getDistType() {
        return distType;
    }

    public void setDistType(String distType) {
        this.distType = distType;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public BigDecimal getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(BigDecimal totalPrice) {
        this.totalPrice = totalPrice;
    }

    public BigDecimal getPayPrice() {
        return payPrice;
    }

    public void setPayPrice(BigDecimal payPrice) {
        this.payPrice = payPrice;
    }

    public BigDecimal getDistPrice() {
        return distPrice;
    }

    public void setDistPrice(BigDecimal distPrice) {
        this.distPrice = distPrice;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<AddOrdersInfoGoodsDto> getGoodsList() {
        return goodsList;
    }

    public void setGoodsList(List<AddOrdersInfoGoodsDto> goodsList) {
        this.goodsList = goodsList;
    }

    public List<OrdersGift> getGiftList() {
        return giftList;
    }

    public void setGiftList(List<OrdersGift> giftList) {
        this.giftList = giftList;
    }

    public String getArriveTime() {
        return arriveTime;
    }

    public void setArriveTime(String arriveTime) {
        this.arriveTime = arriveTime;
    }

    public OrdersAddress getAddress() {
        return address;
    }

    public void setAddress(OrdersAddress address) {
        this.address = address;
    }

    public OrdersPoint getPoint() {
        return point;
    }

    public void setPoint(OrdersPoint point) {
        this.point = point;
    }

    public OrdersCourier getCourier() {
        return courier;
    }

    public void setCourier(OrdersCourier courier) {
        this.courier = courier;
    }

    public Shop getShop() {
        return shop;
    }

    public void setShop(Shop shop) {
        this.shop = shop;
    }

    public Long getPayOutTime() {
        return payOutTime;
    }

    public void setPayOutTime(Long payOutTime) {
        this.payOutTime = payOutTime;
    }

    public String getShipTime() {
        return shipTime;
    }

    public void setShipTime(String shipTime) {
        this.shipTime = shipTime;
    }

    public String getAffirmTime() {
        return affirmTime;
    }

    public void setAffirmTime(String affirmTime) {
        this.affirmTime = affirmTime;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getDistTime() {
        return distTime;
    }

    public void setDistTime(String distTime) {
        this.distTime = distTime;
    }

    public String getDistStatus() {
        return distStatus;
    }

    public void setDistStatus(String distStatus) {
        this.distStatus = distStatus;
    }


    public static OrdersInfoDto news(Orders orders) {
        OrdersInfoDto ordersDto = new OrdersInfoDto();
        ordersDto.setOrdersId(orders.getOrdersId());
        ordersDto.setOrdersNo(orders.getOrdersNo());
        ordersDto.setDistPrice(orders.getDistPrice());
        ordersDto.setPayPrice(orders.getPayPrice());
        ordersDto.setReducePrice(orders.getReducePrice());
        ordersDto.setPayment(orders.getPayment());
        ordersDto.setTotalPrice(orders.getTotalPrice());
        ordersDto.setRemark(orders.getRemark());
        ordersDto.setPayType(orders.getPayType());
        ordersDto.setDistType(orders.getDistType());
        ordersDto.setOrdersType(orders.getOrdersType());
        if(orders.getDistType().equals(OrdersDistTypeEnum.配送.getValue())){
            ordersDto.setArriveTime(orders.getDistTime());
            ordersDto.setAddress(orders.getOrdersAddress());
        }

        if(orders.getDistType().equals(OrdersDistTypeEnum.自取.getValue())){
            ordersDto.setPoint(orders.getOrdersPoint());
            ordersDto.setArriveTime(orders.getPickTime());
        }
        ordersDto.setCourier(orders.getCourier());
        ordersDto.setShop(orders.getShop());

        ordersDto.setStatusCode(getOrdersStatusCode(orders).getCode());
        ordersDto.setStatus(getOrdersStatusCode(orders).getText());
        ordersDto.setCreateTime(orders.getCreateTime());
        ordersDto.setGoodsList(AddOrdersInfoGoodsDto.newsList(orders.getGoodsList()));
        ordersDto.setGiftList(orders.getGiftList());
        ordersDto.setPayOutTime(orders.getPayOutTime().getTime());
        if(orders.getShipTime()!=null){
            ordersDto.setShipTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",orders.getShipTime()));
        }
       if(orders.getAffirmTime()!=null){
           ordersDto.setAffirmTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",orders.getAffirmTime()));
       }
       if(orders.getDistTime30()!=null){
           ordersDto.setDistTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",orders.getDistTime30()));
       }
       ordersDto.setReceiver(orders.getReceiver());
       ordersDto.setDistStatus(orders.getDistStatus());
        return ordersDto;
    }
    public static List<OrdersInfoDto> newsList(List<Orders> ordersList) {
        List<OrdersInfoDto> ordersDtoList = Lists.newArrayList();
        for (Orders orders : ordersList) {
            ordersDtoList.add(news(orders));
        }
        return ordersDtoList;
    }
}
